package cn.udrm.elasticsearch.demo;
import cn.udrm.elasticsearch.fileread.WordReadContent;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class DemoFileTwo {
    public static void main(String[] args) throws Exception {
        File file=new File("C:\\Users\\xurongbei\\Desktop\\documentpass");
        List totalList=new CopyOnWriteArrayList();
        long l =System.currentTimeMillis();
        scanDirNoRecursion("D:\\workspace",totalList);
        System.out.println("============耗时："+(System.currentTimeMillis()-l)+" size："+totalList.size());

    }

    //非递归
    public static void scanDirNoRecursion(String path, List totalList){
        LinkedList list = new LinkedList();
        File dir = new File(path);
        File file[] = dir.listFiles();
        for (int i = 0; i < file.length; i++) {
            if (file[i].isDirectory())
                list.add(file[i]);
            else{
                totalList.add(file[i]);
            }
        }
        File tmp;
        while (!list.isEmpty()) {
            tmp = (File)list.removeFirst();//首个目录
            if (tmp.isDirectory()) {
                file = tmp.listFiles();
                if (file == null)
                    continue;
                for (int i = 0; i < file.length; i++) {
                    if (file[i].isDirectory())
                        list.add(file[i]);//目录则加入目录列表，关键
                    else{

                        totalList.add(file[i]);
                    }
                }
            } else {
                totalList.add(tmp);
            }
        }
    }

    private static String readLine(File file){
        BufferedReader bufferedReader = null;
        StringBuffer stringBuffer=new StringBuffer();
        if(!file.exists()){
            System.out.println("文件不存在");
        } else {
            try {
                //设置文件的编码，否则会乱码
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
            } catch (UnsupportedEncodingException | FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String lineText = null;
            try {
                while((lineText = bufferedReader.readLine()) != null){
                    if (lineText != null && !lineText.equals("")){
                       stringBuffer.append(lineText);

                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuffer.toString();
    }

    private static long readByChannel() throws IOException {
        long counts = 0;
        File file = new File("bigFile.txt");
        FileInputStream fis = new FileInputStream(file);
        FileChannel fc = fis.getChannel();
        ByteBuffer bbuf = ByteBuffer.allocate(2048);
        int offset = 0;
        while((offset = fc.read(bbuf)) != -1) {
            counts = counts + offset;
            bbuf.clear();
        }
        fc.close();
        fis.close();
        return counts;
    }
}
